<script setup>
defineOptions({ name: 'UserPassword' })
import CardPage from '@/components/CardPage.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import 'element-plus/dist/index.css'
import { userUpdatePasswordService } from '@/api/user'
const formRef = ref(null)
// `form` 数据对象
const form = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})
// 表单校验规则
const rules = {
  old_pwd: [
    { required: true, message: '旧密码不能为空', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '旧密码长度需为6-15位', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '新密码不能为空', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '旧密码长度需为6-15位', trigger: 'blur' }
  ],
  re_pwd: [
    { required: true, message: '确认密码不能为空', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== form.value.new_pwd) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 提交表单方法function onSubmit() {
const onSubmit = async () => {
  const valid = await formRef.value?.validate()
  if (!valid) return
  console.log(form.value)
  const res = await userUpdatePasswordService(form.value)
  ElMessage.success(res.message)
}
// 重置表单方法
const resetForm = () => {
  formRef.value.resetFields()
}
</script>
<template>
  <card-page title="重置密码">
    <el-form
      label-width="100px"
      class="user-password-form"
      :model="form"
      :rules="rules"
      ref="formRef"
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input
          type="password"
          placeholder="请输入原密码"
          v-model="form.old_pwd"
        />
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input
          type="password"
          placeholder="请输入新密码"
          v-model="form.new_pwd"
        />
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input
          type="password"
          placeholder="请再次输入新密码"
          v-model="form.re_pwd"
        />
      </el-form-item>
      <el-button type="primary" class="sub-button" @click="onSubmit"
        >更新密码</el-button
      >
      <el-button @click="resetForm">重置</el-button>
    </el-form>
  </card-page>
</template>
<style lang="scss" scoped>
.user-password-form {
  width: 1500px;
  margin-top: 12px;
  .el-form-item {
    height: 3rem;
    align-items: center;
    .el-form-item__content {
      .el-input {
        height: 2.5rem;
      }
    }
  }
}
.sub-button {
  margin-left: 230px;
  margin-right: 20px;
}
</style>
